#!/bin/bash

source /home/shell/util.sh

function install () {
    if [[ "$(ostype)" == "CentOS" ]] && [[ ! -f "/usr/sbin/vsftpd" ]]; then
        yum install -y vsftpd
        systemctl enable --now vsftpd
        config_path=/etc/vsftpd/vsftpd.conf
        setenforce 0
        sudo sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
    elif [[ "$(ostype)" == "Ubuntu" ]] && [[ ! -f "/usr/sbin/vsftpd" ]]; then
        apt install -y vsftpd libpam0g-dev db-util
        config_path=/etc/vsftpd.conf
    else
        printFail "未知操作系统$(ostype)"
        exit;
    fi
    
    if [ ! -d "/etc/vsftpd" ]; then
        mkdir -p /etc/vsftpd
    fi

    if [ ! -d "/etc/vsftpd/users/" ]; then
        mkdir -p /etc/vsftpd/users/
    fi

    touch /etc/vsftpd/virtusers

    db_load -T -t hash -f /etc/vsftpd/virtusers /etc/vsftpd/virtusers.db
    chmod 600 /etc/vsftpd/virtusers.db

tee /etc/pam.d/vsftpd<<EOF
auth    required        pam_userdb.so   db=/etc/vsftpd/virtusers
account required        pam_userdb.so   db=/etc/vsftpd/virtusers
EOF

tee ${config_path} -a<<EOF
user_config_dir=/etc/vsftpd/users
guest_enable=YES
guest_username=vuser
virtual_use_local_privs=YES
allow_writeable_chroot=YES
EOF

    sed -i "s@#ascii_upload_enable=YES@ascii_upload_enable=YES@g" ${config_path}
    sed -i "s@#ascii_download_enable=YES@ascii_download_enable=YES@g" ${config_path}
    sed -i "s@#xferlog_file=/var/log/xferlog@xferlog_file=/var/log/xferlog@g" ${config_path}
    sed -i "s@#chroot_list_enable=YES@chroot_list_enable=YES@g" ${config_path}
    sed -i "s@#chroot_local_user=YES@chroot_local_user=NO@g" ${config_path}
    if [[ "$(ostype)" == "CentOS" ]]; then
        sed -i "s@#chroot_list_file=/etc/vsftpd/chroot_list@chroot_list_file=/etc/vsftpd/chroot_list@g" ${config_path}
    elif [[ "$(ostype)" == "Ubuntu" ]]; then
        sed -i "s@#chroot_list_file=/etc/vsftpd.chroot_list@chroot_list_file=/etc/vsftpd/chroot_list@g" ${config_path}
    else
        printWarn "疑似不支持系统"
    fi

    cp ${config_path} /etc/vsftpd/vsftpd.conf.bak
    firewall-addservice ftp
    systemctl restart vsftpd
}

function addUser () {
    read -p "ftp username[wxx]：" ftpuser
    ftpuser=${ftpuser:-"wxx"};
    read -p "ftp password[123456]：" ftpasswd
    ftpasswd=${ftpasswd:-"123456"};
    read -p "ftp homedir[/home/shell]：" ftpdir
    ftpdir=${ftpdir:-"/home/shell"};

    useradd -d ${ftpdir} -s /sbin/nologin vuser

tee /etc/vsftpd/virtusers -a<<EOF
${ftpuser}
${ftpasswd}
EOF

tee /etc/vsftpd/users/${ftpuser}<<EOF
write_enable=YES
anon_world_readable_only=NO
anon_other_write_enable=YES
anon_umask=022
local_root=${ftpdir}
EOF

tee /etc/vsftpd/chroot_list -a<<EOF
${ftpuser}
EOF

    db_load -T -t hash -f /etc/vsftpd/virtusers /etc/vsftpd/virtusers.db
    chmod 600 /etc/vsftpd/virtusers.db

    chmod 770 ${ftpdir}
    chown vuser:vuser ${ftpdir}

    systemctl restart vsftpd

    printInfo "=========================安装信息========================="
    printInfo "ftp用户：${ftpuser}"
    printInfo "ftp目录：${ftpdir}"
    printInfo "版本：`vsftpd -version`"
    printInfo "=========================安装信息========================="

}


if [ ! -f "/usr/sbin/vsftpd" ]; then
    install
fi
addUser